package com.kmproject.haifu.app.service;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.jess.arms.base.BaseService;
import com.kmproject.haifu.R;
import com.kmproject.haifu.mvp.ui.activity.HomeActivity;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * 定位服务
 */
public class HfLocationService extends BaseService {
    private Intent myIntent = null;
    private boolean isLoop = true;//启动服务的时候是否循环定位，默认true
    private String type = Type.NETWORK_GPS;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        myIntent = intent;
        return super.onBind(intent);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            if (intent.hasExtra("isLoop")) {
                isLoop = intent.getBooleanExtra("isLoop", true);
            }
            if (intent.hasExtra("type")) {
                type = intent.getStringExtra("type");
            }
        }

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void init() {
        if (myIntent != null) {
            if (myIntent.hasExtra("isLoop")) {
                isLoop = myIntent.getBooleanExtra("isLoop", true);
            }
            if (myIntent.hasExtra("type")) {
                type = myIntent.getStringExtra("type");
            }
        }

        if (Type.GOOGLE_PLACE_API.equals(type)) {
            GoogleLocation.getInstance(this).setLoop(isLoop).findCurrentPlace();
        } else {
            GoogleLocation.getInstance(this).setLoop(isLoop).startLocation();
        }

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        notificationFor8();
//        }
    }

    /**
     * 针对安卓8.0新特性阻止后台持续定位，使用前台服务
     */
    public void notificationFor8() {
        //开启前台定位服务：
        Notification.Builder builder = new Notification.Builder(getApplicationContext());
        //获取一个Notification构造器
        Intent nfIntent = new Intent(HfLocationService.this.getApplicationContext(), HomeActivity.class);
        builder.setContentIntent(PendingIntent.getActivity(HfLocationService.this, 0, nfIntent, 0)) // 设置PendingIntent
                .setContentTitle("正在进行后台定位") // 设置下拉列表里的标题
                .setSmallIcon(R.drawable.user_app_icon) // 设置状态栏内的小图标
                .setContentText("后台定位通知") // 设置上下文内容
                .setAutoCancel(true)
                .setWhen(System.currentTimeMillis()); // 设置该通知发生的时间
        Notification notification = null;
        notification = builder.build();
        notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
//        mLocationClient.enableLocInForeground(1001, notification);// 调起前台定位
////停止前台定位服务：
//        mLocationClient.disableLocInForeground(true);// 关闭前台定位，同时移除通知栏
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        GoogleLocation.getInstance(this).stopLocation();
    }

    @Retention(RetentionPolicy.SOURCE)
    public @interface Type {
        /**
         * Android框架自带的网络辅助Gps定位
         */
        String NETWORK_GPS = "network8gps";
        /**
         * 调用谷歌地图开放平台的接口获取当前位置信息（会取建议列表中的第一条数据作为当前位置）
         */
        String GOOGLE_PLACE_API = "googlePlaceApi";
    }
}


